package com.lk.ii.controller;

import com.lk.common.annotation.Log;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.core.page.TableDataInfo;
import com.lk.common.enums.BusinessType;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.framework.util.ShiroUtils;
import com.lk.ii.domain.IiAdMaterial;
import com.lk.ii.domain.IiMaterial;
import com.lk.ii.service.IIiAdMaterialService;
import com.lk.ii.service.IIiMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位详情Controller
 *
 * @author lk
 * @date 2021-02-05
 */
@Controller
@RequestMapping("/ii/ad_material")
public class IiAdMaterialController extends BaseController {
    private String prefix = "ii/ad_material";

    @Autowired
    private IIiAdMaterialService iiAdMaterialService;

    @Autowired
    private IIiMaterialService iiMaterialService;

    @RequiresPermissions("ii:ad_material:view")
    @GetMapping("/index/{adId}")
    public String ad_material(@PathVariable("adId") Long adId,ModelMap mmap)
    {
        mmap.put("adId", adId);
        return prefix + "/ad_material";
    }

    /**
     * 查询广告位详情列表
     */
    @RequiresPermissions("ii:ad_material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiAdMaterial iiAdMaterial) {
        startPage();
        List<IiAdMaterial> list = iiAdMaterialService.selectIiAdMaterialList(iiAdMaterial);
        for (IiAdMaterial m : list) {
            IiMaterial iiMaterial = iiMaterialService.selectIiMaterialById(m.getMaterialId());
            m.setMaterialName(iiMaterial.getName());
            m.setMaterialUrl(iiMaterial.getUrl());
        }
        return getDataTable(list);
    }

    /**
     * 导出广告位详情列表
     */
    @RequiresPermissions("ii:ad_material:export")
    @Log(title = "广告位详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{adId}")
    @ResponseBody
    public AjaxResult export(@PathVariable("adId") Long adId, IiAdMaterial iiAdMaterial) {
        iiAdMaterial.setAdId(adId);
        List<IiAdMaterial> list = iiAdMaterialService.selectIiAdMaterialList(iiAdMaterial);
        ExcelUtil<IiAdMaterial> util = new ExcelUtil<IiAdMaterial>(IiAdMaterial.class);
        return util.exportExcel(list, "ad_material");
    }

    /**
     * 新增广告位详情
     */
    @GetMapping("/add/{adId}")
    public String add(@PathVariable("adId") Long adId,ModelMap mmap)
    {
        mmap.put("adId", adId);
        return prefix + "/add";
    }

    /**
     * 新增保存广告位详情
     */
    @RequiresPermissions("ii:ad_material:add")
    @Log(title = "广告位详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiAdMaterial iiAdMaterial)
    {
        iiAdMaterial.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiAdMaterialService.insertIiAdMaterial(iiAdMaterial));
    }

    /**
     * 修改广告位详情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        IiAdMaterial iiAdMaterial = iiAdMaterialService.selectIiAdMaterialById(id);
        IiMaterial iiMaterial = iiMaterialService.selectIiMaterialById(iiAdMaterial.getMaterialId());
        iiAdMaterial.setMaterialName(iiMaterial.getName());
        iiAdMaterial.setMaterialUrl(iiMaterial.getUrl());
        mmap.put("iiAdMaterial", iiAdMaterial);
        return prefix + "/edit";
    }

    /**
     * 修改保存广告位详情
     */
    @RequiresPermissions("ii:ad_material:edit")
    @Log(title = "广告位详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiAdMaterial iiAdMaterial) {
        iiAdMaterial.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiAdMaterialService.updateIiAdMaterial(iiAdMaterial));
    }

    /**
     * 删除广告位详情
     */
    @RequiresPermissions("ii:ad_material:remove")
    @Log(title = "广告位详情", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(iiAdMaterialService.deleteIiAdMaterialByIds(ids));
    }
}
